package ktwl.code.connection;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.logging.Logger;

/**
 * 连接池
 *
 * @author zhuhe on 2019/8/21 14:49
 * @version 1.0
 */
public class CustomDataSource implements DataSource {

    /**创建一个存放多个连接的容器,因为需要经常删除,查找.这里用LinkedList容器*/
    private static final LinkedList<Connection> POOL = new LinkedList<Connection>();

    static{
        for (int i = 0; i < 5; i++) {
            Connection conn= JdbcConnection.getConnection();
            if(conn != null) {
                POOL.add(conn);
            }
        }
    }

    /**
      * 获取连接 on 2019/8/21 15:31
      *
      * @return connection
      * @throws SQLException SQLException
      */
    @Override
    public Connection getConnection() throws SQLException {
        Connection conn=null;
        //在使用POOL这个变量之前需要判断POOL是否为空,若为空需要重新创建
        if (POOL.size() == 0) {
            for (int i = 0; i < 5; i++) {
                conn = JdbcConnection.getConnection();
                if(conn != null) {
                    POOL.add(conn);
                }
            }
        }
        conn = POOL.remove(0);
        return conn;
    }

    /**
      * 把当前连接归还给连接池 on 2019/8/21 15:29
      *
      * @param conn 要归还的连接
      */
    public void addBack(Connection conn){
        //最大连接数,大于最大连接数时关闭连接
        if(POOL.size() < 30) {
            POOL.add(conn);
        }else{
            JdbcConnection.release(conn,null,null,null);
        }
    }

    /**
      * 关闭连接池 on 2019/8/21 16:57
      */
    public void close(){
        Iterator it = POOL.iterator();
        Connection conn=null;
        while(it.hasNext()){
            conn=(Connection)it.next();
            JdbcConnection.release(conn,null,null,null);
            it.remove();
        }
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int getLoginTimeout() throws SQLException {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void setLogWriter(PrintWriter arg0) throws SQLException {
        // TODO Auto-generated method stub

    }

    @Override
    public void setLoginTimeout(int arg0) throws SQLException {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean isWrapperFor(Class<?> arg0) throws SQLException {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public <T> T unwrap(Class<T> arg0) throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Connection getConnection(String arg0, String arg1) throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }
}